var SiteAPI =  {
    params : {
        /**matomo API接口必须参数 */
        token_auth: piwik.token_auth,
        force_api_session: 1,
        "format": 'json',
            //idSite: piwik.idSite || broadcast.getValueFromUrl('idSite'),
            //period: piwik.period || broadcast.getValueFromUrl('period'),
    },
    _getFormData: function(param){
        var formData = new FormData();
        for (var p in param) {
            formData.append(p, param[p])
        }
        return formData;  
    },
    // 发送axios请求
    _axiosPost: function (url, param) {
        axios.defaults.withCredentials = true;
        return new Promise((resolve, reject) => {
            axios.post(url,
                SiteAPI._getFormData(param)
            ).then((response) => {
                resolve(response.data)
            }).catch(ret => {
                reject(ret)
            })
        })
    },
    _formartSiteData:function(that ){
        var param = Object.assign({
            "siteName": that.formData.name,
            "urls": that.formData.domain,
            "type": that.addtype == 1 ? "website" : 'adplace',
            "isuser": 1,
            "ecommerce":1,
            "searchCategoryParameters": that.formData.industry.join("/"),
            "format": 'json',
        }, SiteAPI.params)

        var meta = {
            'meta[beian]': that.formData.beian,
            'meta[visitors]': that.formData.visitors,
            'meta[smalltext]': that.formData.smalltext,
        }

        return {'param': param, 'meta': meta}
    },
    // 添加数据到 站点
    addSite: function(  that ) {
     
        var data = SiteAPI._formartSiteData(that);
        var param = data.param;
        var meta = data.meta;
 
        //================
       return SiteAPI._axiosPost('index.php?module=API&method=SitesManager.addSite', param).then((data)=>{
            if(data['value']>0){
                meta.idSite =  data['value'];
               return SiteAPI.updateSiteMeta(  meta);
            }
        })
    
    },
    // 修改站点信息
    updateSite: function(id, that) {

        var data = SiteAPI._formartSiteData(that);
        var param = data.param;
        var meta = data.meta;
        // 赋值需要修改 的记录id
        param.idSite =  id;
        meta.idSite = id;
        //================
        return SiteAPI._axiosPost('index.php?module=API&method=SitesManager.updateSite', param).then((data) => {
            // console.log(data);
           return SiteAPI.updateSiteMeta(  meta);
            
        })
    },
    // 更新站点数据的meta字段值
    updateSiteMeta: function( meta)
    {
        
        var param = Object.assign({ "format": 'json', ...meta}, SiteAPI.params);
 
        return SiteAPI._axiosPost('index.php?module=API&method=AdsPlace.updateSiteMeta' , param);
        
    },
    // 通过idSite 获取 站点数据
    getSiteFromId: function( idSite ) {
        var param = Object.assign({ 
            'idSite': idSite, 
            "format": 'json'
        }, SiteAPI.params);
        return SiteAPI._axiosPost('index.php?module=API&method=SitesManager.getSiteFromId', param); 
  
    },
    getAllSites: function() {
        var param = Object.assign({          
            "format": 'json'
        }, SiteAPI.params);
        return SiteAPI._axiosPost('index.php?module=API&method=AdsPlace.getAllSites', param);
      
    },
    deleteSite: function(idSite){
        var param = Object.assign({
            "idSite": idSite,
            "format": 'json'
        }, SiteAPI.params);
       return SiteAPI._axiosPost('index.php?module=API&method=SitesManager.deleteSite', param);
    },
  
}

export default SiteAPI;